﻿@page "/fetchdata"

@using BootstrapMaBla.Data
@inject WeatherForecastService ForecastService

<h1>天气温度图表</h1>

<Chart @ref="LineChart" OnInitAsync="() => OnInit(0.4f, false)" />

@code {

    private WeatherForecast[] forecasts;

    private Chart? LineChart { get; set; }

    protected override async Task OnInitializedAsync()
    {
        forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
    }

    private Task<ChartDataSource> OnInit(float tension, bool hasNull)
    {
        var ds = new ChartDataSource();
        ds.Options.Title = "天气温度折线图";
        ds.Options.X.Title = "日期";
        ds.Options.Y.Title = "温度";
        ds.Labels = forecasts.Select(x => $"{x.Date:M}");
        ds.Data.Add(new ChartDataset()
            {
                Tension = tension,
                Label = $"温度趋势",
                Data = forecasts.Select(x => (object)x.TemperatureC)
            });

        return Task.FromResult(ds);
    }
}
